home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 38 / Amiga Format CD38 (1999-03-15)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-04].iso / -seriously_amiga- / misc / football / exec / scheduler.rexx < prev    next >
OS/2 REXX Batch file  |  1999-02-03  |  18KB  |  565 lines

  1. /* ***********************************************************************
  2.  
  3.    SCHEDULER PROGRAM FOR FOOTBALL REXX SUITE
  4.   -------------------------------------------
  5.                    Copyright  Mark Naughton 1996
  6.  
  7.  
  8. Version    Date     History
  9. --------------------------------------------------------------------------
  10.  1.0       090996   First release.
  11.            180996   Changed record-count so that it is stored in element
  12.                     zero of each array. Deleted some variable assigns
  13.                     that weren't needed. Amended reading into arrays so
  14.                     that the count is set correctly for one element.
  15.                     Removed all "call "references before library routines.
  16.            270996   Added code to sort 'Teams.df' and to recreate it.
  17.                     Changed a few messages and tidied up.
  18.            ******   Decided not to store goalscorers. Was not viable as
  19.                     too much data would have to be collected.
  20.            270996   Added code for points_per_draw and points_per_loss.
  21.                     Now uses DOS Delete command, not the support function.
  22.                     Amended reading in of teams - quicker, tidier. Added
  23.                     code to display a report on the league. Updated to
  24.                     use new filenames. Improved error messages. Added
  25.                     error check for more than 1000 matches.
  26.  1.1       131196   Added argument so it can become a component of
  27.                     FOOTBALL. Removed all messages. Added checks for
  28.                     files - if not found, exits without a message.
  29.                     Rewrote report.
  30.            201196   Added code to store relegation. All this will do is
  31.                     display a line across the league table. Amended msg
  32.                     for play_each_other. Added code to allow teams to
  33.                     play each other more than twice.
  34.            201196   Decided not to implement PreviousPosition as it would
  35.                     require file changes and a reformat of the league
  36.                     table. It may be implemented soon though.
  37.            211196   Updated and tidied the display.
  38.            201296   Fixed bug where if the number of times that each team
  39.                     play each other is an even number, then its not set
  40.                     to the correct value (only works for odd matches).
  41.            110197   Blanks are now removed from the filename, before
  42.                     creation.
  43.  1.2       110497   Added code to support Points_Per_Goals.
  44.  2.0       010997   Have been working on Scheduling for about a week. Put
  45.                     in code and now ready to test.
  46.            030997   Problem with format of '.sf' file - fixed. Program
  47.                     failed due to missing 'do' - fixed. All works.
  48.            050997   Added extra '*' at end of '.sf' file in line with
  49.                     manual created one.
  50.            070997   Fixed bug for AUTOSCHED - no. of matches in report was
  51.                     wrong - needed to be reset before the loop.
  52.            220997   Added code for promotion and divisions.
  53.            240997   Removed all data such as teams count, points per win,
  54.                     relegation etc, as they are already stored in '.df'.
  55.            250997   Wasn't creating the files - traced to resetting the
  56.                     strings that were used to pick the number of divisions
  57.                     and the divisions themselves, to the variables that
  58.                     are used as the assignments/counters. Made amendments
  59.                     to display of the report.
  60.            151297   Tidied display.
  61.            240898   Amended code that regenerates .df file as it was
  62.                     adding two blank lines before the teams and causing
  63.                     it to fail.
  64.  
  65. **************************************************************************
  66.  
  67. Procedure
  68. ---------
  69.  
  70. 1. Check files exist.
  71. 2. Read 'Teams.df' and write teams to output file.
  72. 3. If AutoSched is specified then store parameters.
  73. 4. Using external program, sort the teams.
  74. 5. Re-read the teams and recreate 'Teams.df'.
  75. 6. Open 'Teams.df' for reading.
  76. 7. Extract data and store.
  77. 8. If no separator in it, then take it as a
  78.    team. Strip blanks and store.
  79. 9. Close input file.
  80. 10.IF AUTOMATIC - Read specified schedule file.
  81. 11.Read schedules and store dates/weeks.
  82. 12.Give error if the number of teams does not equal
  83.    the number of lines in the schedule file.
  84. 13.Write the dates/weeks to a file then use an
  85.    external program to sort them, writing back to the file.
  86. 14.Read dates into array and delete file.
  87. 15.Format dates array with season start date.
  88. 16.Format and write '.sf' files.
  89. 17.IF MANUAL - Open 'Teams.sf' file.
  90. 18.Write league name to it.
  91. 19.Loop and write all teams with a schedule of
  92.    all other teams. Number of matches is stored.
  93. 20.Close output file.
  94. 21.Open 'Teams.stats' for writing.
  95. 22.Write all data such as PLAYED,WIN,DRAW,LOST,GOALS_FOR,GOALS_AGAINST and
  96.    POINTS for each team.
  97. 23.Close file, create a report on the league created and then exit.
  98.  
  99. ************************************************************************** */
  100. PARSE ARG league_file
  101.  
  102. version      = 2
  103. league_file  = "Data/" || strip(league_file)
  104. input_file   = '.df'
  105. output_file  = '.sf'
  106. output2_file = '.stats'
  107. output3_file = '.temp'
  108. title        = '*LEAGUE_NAME='
  109. points_win   = '*POINTS_PER_WIN='
  110. points_drw   = '*POINTS_PER_DRW='
  111. points_lse   = '*POINTS_PER_LSE='
  112. points_gls   = '*POINTS_PER_GLS='
  113. releg        = '*RELEGATION='
  114. playother    = '*PLAY_OTHER='
  115. promoted     = '*PROMOTED='
  116. num_divs     = '*NUM_DIVISIONS='
  117. divisions    = '*DIVISIONS='
  118. pkauthor     = '*  Author ='
  119. pkversion    = '*  Version='
  120. separator    = '*'
  121. teams.       = '???'
  122. tcount       = 0
  123. ttc          = 0
  124. ptsgls       = 0
  125. input2_file  = '.schd'
  126. separator    = '*'
  127. not_played   = '__   __'
  128. dateweeks.   = '???'
  129. tdateweeks.  = '???'
  130. schedules.   = '???'
  131. months       = "January February March April May June July August September October November December"
  132. autosched    = "*AUTOSCHD="
  133. autos        = 0
  134. ndivs        = 0
  135. promo        = 0
  136. divs         = ''
  137.  
  138.  
  139. if exists(league_file || input_file) = 0 then exit
  140.  
  141. tcount = 0
  142. if open(datafile,league_file || input_file,'r') then do
  143.    if open(datafile2,league_file || output3_file,'w') then do
  144.       do while ~eof(datafile)
  145.          line = readln(datafile)
  146.          line = strip(line)
  147.          if pos(separator,line) > 0 & line ~='' then do
  148.             if pos(autosched,line) > 0 then do
  149.                autofile = delstr(line,1,10)
  150.                autos = 1
  151.             end
  152.             tcount = tcount + 1
  153.             teams.tcount = line
  154.          end
  155.          else do
  156.             ttc = ttc + 1
  157.             writeln(datafile2,line)
  158.          end
  159.       end
  160.       close(datafile2)
  161.    end
  162.    else do
  163.       say
  164.       say "ERROR :    (Scheduler)"
  165.       say
  166.       say "Cannot write to '"league_file||output3_file"'."
  167.       close(datafile)
  168.       exit
  169.    end
  170.    close(datafile)
  171. end
  172. else do
  173.    say
  174.    say "ERROR :    (Scheduler)"
  175.    say
  176.    say "Cannot open '"league_file || input_file"' for reading/sorting."
  177.    exit
  178. end
  179.  
  180. address command 'sort 'league_file || output3_file league_file || output3_file
  181.  
  182. if open(datafile2,league_file || output3_file,'r') then do
  183.    if open(datafile,league_file || input_file,'w') then do
  184.       do i=1 to tcount
  185.          if teams.i ~= '' then
  186.             writeln(datafile,teams.i)
  187.       end
  188.       do j=1 to ttc                   /* while ~eof(datafile2)  */
  189.          line = readln(datafile2)
  190.          line = strip(line)
  191.          if line ~= '' then do
  192.             if j = ttc then
  193.                writech(datafile,line)
  194.             else
  195.                writeln(datafile,line)
  196.          end
  197.       end
  198.       close(datafile)
  199.    end
  200.    else do
  201.       say
  202.       say "ERROR :    (Scheduler)"
  203.       say
  204.       say "Cannot write to '"league_file||input_file"'."
  205.       close(datafile2)
  206.       exit
  207.    end
  208.    close(datafile2)
  209. end
  210. else do
  211.    say
  212.    say "ERROR :    (Scheduler)"
  213.    say
  214.    say "Cannot open '"league_file || output3_file"' for reading/sorting."
  215.    exit
  216. end
  217.  
  218. address command 'delete >NIL: 'league_file || output3_file
  219.  
  220. tcount = 0
  221. if open(datafile,league_file || input_file,'r') then do
  222.    do while ~eof(datafile)
  223.       line = readln(datafile)
  224.       if pos(title,line) > 0 then        league_title = delstr(line,1,13)
  225.       if pos(pkauthor,line) > 0 then     author = delstr(line,1,12)
  226.       if pos(pkversion,line) > 0 then    fversion = delstr(line,1,12)
  227.       if pos(points_win,line) > 0 then   ptswin=delstr(line,1,16)
  228.       if pos(points_drw,line) > 0 then   ptsdrw=delstr(line,1,16)
  229.       if pos(points_lse,line) > 0 then   ptslse=delstr(line,1,16)
  230.       if pos(points_gls,line) > 0 then   ptsgls=delstr(line,1,16)
  231.       if pos(releg,line) > 0 then        reg = delstr(line,1,12)
  232.       if pos(playother,line) > 0 then    playo = delstr(line,1,12)
  233.       if pos(promoted,line) > 0 then     promo = delstr(line,1,10)
  234.       if pos(num_divs,line) > 0 then     ndivs = delstr(line,1,15)
  235.       if pos(divisions,line) > 0 then    divs  = delstr(line,1,11)
  236.       if pos(separator,line) = 0 then do
  237.          line = strip(line)
  238.          tcount       = tcount + 1
  239.          teams.tcount = line
  240.       end
  241.    end
  242.    close(datafile)
  243. end
  244. else do
  245.    say
  246.    say "ERROR :    (Scheduler)"
  247.    say
  248.    say "Unable to open '"league_file || input_file"' file."
  249.    exit
  250. end
  251.  
  252. if ndivs > 1 then do
  253.    if words(divs)+1 ~= ndivs then do
  254.       say
  255.       say "ERROR :"
  256.       say
  257.       say "The number of divisions specified does not match the"
  258.       say "number selected. The number of divisions specified"
  259.       say "must include the ones selected BUT also, the one that"
  260.       say "you are creating. ie, Three divisions specified, so you"
  261.       say "change the number of divisions to FOUR."
  262.       say
  263.       say "In this league that you have tried to create, you have specified"
  264.       say "words(divs)" leagues but the number was set to "ndivs"."
  265.       say "The number should have been '"words(divs)+1"' leagues."
  266.       say
  267.       say "Schedule creation aborted."
  268.       exit
  269.    end
  270.  
  271.    fd = 0
  272.    do i=1 to ndivs-1
  273.       if exists("Data/"||word(divs,i)||input_file) = 0 then do
  274.          if fd = 0 then do
  275.             say
  276.             say "ERROR :    (Scheduler)"
  277.             say
  278.          end
  279.          say "League '"word(divs,i)"' does not exist."
  280.          fd = fd + 1
  281.       end
  282.    end
  283.    if fd > 0 then
  284.       exit
  285. end
  286.  
  287.                                           /* Automatic Schedule creation */
  288. if autos = 1 then do
  289.    if exists("Data/"autofile||input2_file) = 0 then do
  290.       say
  291.       say "ERROR :    (Scheduler)"
  292.       say
  293.       say "Cannot find 'Data/"autofile||input2_file"'."
  294.       exit
  295.    end
  296.  
  297.    ct = 0
  298.    sct= 0
  299.    weeks = 0
  300.    dates = 0
  301.  
  302.    if open(datafile,"Data/"autofile||input2_file,'r') then do
  303.       do while ~eof(datafile)
  304.          line = readln(datafile)
  305.          if pos("*WEEKS",line) then do
  306.             weeks=1
  307.          end
  308.          if pos("*DATES=",line) then do
  309.             startdate=substr(line,8,8)
  310.             dates = 1
  311.          end
  312.          if line ~= '' & pos(separator,line) = 0 then do
  313.             sct = sct + 1
  314.             schedules.sct = line
  315.             counter = words(line)
  316.             do i=1 to counter
  317.                sdate = word(line,i)
  318.                if sdate = 0 then
  319.                   iterate
  320.                if ct = 0 then do
  321.                   ct = ct + 1
  322.                   dateweeks.ct = sdate
  323.                end
  324.                else do
  325.                   ij = 0
  326.                   do j=1 to ct
  327.                      if sdate = dateweeks.j then do
  328.                         ij = 1
  329.                         leave
  330.                      end
  331.                   end
  332.                   if ij = 0 then do
  333.                      ct = ct + 1
  334.                      dateweeks.ct = sdate
  335.                   end
  336.                end
  337.             end
  338.          end
  339.       end
  340.       close(datafile)
  341.    end
  342.    else do
  343.       say
  344.       say "ERROR :    (Scheduler)"
  345.       say
  346.       say "Cannot find 'Data/"autofile||input2_file"' for reading."
  347.       exit
  348.    end
  349.  
  350.    if tcount ~= sct then do
  351.       say
  352.       say "ERROR :    (Scheduler)"
  353.       say
  354.       say "The schedule file specified only has "sct" scheduled"
  355.       say "teams whereas there are "counter" teams in the league."
  356.       say
  357.       say "Creation of files aborted. Re-specify correct schedule."
  358.       say
  359.       exit
  360.    end
  361.  
  362.    if open(datafile2,"RAM:schd.temp",'w') then do
  363.       do j=1 to ct
  364.          writeln(datafile2,dateweeks.j)
  365.       end
  366.       close(datafile2)
  367.    end
  368.    else do
  369.       say
  370.       say "ERROR :    (Scheduler)"
  371.       say
  372.       say "Cannot create temporary file in RAM:."
  373.       exit
  374.    end
  375.  
  376.    if weeks = 1 then
  377.       address command 'sort RAM:schd.temp RAM:schd.temp'
  378.    if dates = 1 then
  379.       address command 'Exec/SortWkDts'
  380.  
  381.    if open(datafile2,"RAM:schd.temp",'r') then do
  382.       do j=1 to ct
  383.          dateweeks.j = readln(datafile2)
  384.       end
  385.       close(datafile2)
  386.    end
  387.    else do
  388.       say
  389.       say "ERROR :    (Scheduler)"
  390.       say
  391.       say "Cannot find temporary file in RAM:."
  392.       exit
  393.    end
  394.  
  395.    address command 'delete >NIL: RAM:schd.temp'
  396.  
  397.    if dates = 1 then do
  398.       do j=1 to ct
  399.          if dateweeks.j = startdate then do
  400.             say j-1
  401.             do i=1 to j-1
  402.                tdateweeks.i = dateweeks.i
  403.             end
  404.             k = 1
  405.             do i=j to ct
  406.                dateweeks.k = dateweeks.i
  407.                k = k + 1
  408.             end
  409.             do i=1 to j-1
  410.                dateweeks.k = tdateweeks.i
  411.                k = k + 1
  412.             end
  413.             leave
  414.          end
  415.       end
  416.    end
  417.  
  418.    if open(outfile,league_file || output_file,"w") then do
  419.       writeln(outfile,"*")
  420.       writeln(outfile,"**" league_title)
  421.       writeln(outfile,"*")
  422.       writeln(outfile,"*")
  423.  
  424.       matches = 0
  425.       do i=1 to ct
  426.          if weeks = 1 then
  427.             writeln(outfile,"*Week: "dateweeks.i)
  428.          if dates = 1 then do
  429.             mnth = substr(dateweeks.i,3,2)
  430.             ndate= substr(dateweeks.i,5,4)||mnth||substr(dateweeks.i,1,2)
  431.             weekd= date('w',ndate,'s')
  432.             writeln(outfile,"*Date: "weekd" "substr(dateweeks.i,1,2)" "word(months,mnth)" "substr(dateweeks.i,5,4))
  433.          end
  434.          writeln(outfile,"*")
  435.          do k=1 to counter
  436.             do j=1 to counter
  437.                sdate = word(schedules.k,j)
  438.                if dateweeks.i = sdate then do
  439.                   writech(outfile,left(teams.k,30))
  440.                   writeln(outfile," __   __ " teams.j)
  441.                   matches = matches + 1
  442.                   leave
  443.                end
  444.             end
  445.          end
  446.          writeln(outfile,"*")
  447.       end
  448.       writeln(outfile,"*")
  449.       close(outfile)
  450.    end
  451.    else do
  452.       say
  453.       say "ERROR :    (Scheduler)"
  454.       say
  455.       say "Unable to write to '"league_file || output_file"'."
  456.       exit
  457.    end
  458. end
  459.                                           /* Manual Schedule creation */
  460. if autos = 0 then do
  461.    if open(outfile,league_file || output_file,"w") then do
  462.       writeln(outfile,"*")
  463.       writeln(outfile,"**" league_title)
  464.       writeln(outfile,"*")
  465.       writeln(outfile,"*")
  466.  
  467.       increm = playo / 2
  468.       matches = 0
  469.       do k=1 to increm
  470.          do i=1 to tcount
  471.             do j=1 to tcount
  472.                if j~=i then do
  473.                    writech(outfile,left(teams.i,30))
  474.                    writeln(outfile," __   __ " teams.j)
  475.                    matches = matches + 1
  476.                end
  477.             end
  478.             writeln(outfile,"*")
  479.             if i ~= tcount then
  480.                writeln(outfile,"*")
  481.          end
  482.       end
  483.       writech(outfile,"*")
  484.       close(outfile)
  485.    end
  486.    else do
  487.       say
  488.       say "ERROR :    (Scheduler)"
  489.       say
  490.       say "Unable to write to '"league_file || output_file"'."
  491.       exit
  492.    end
  493. end
  494.  
  495. if open(outfile,league_file || output2_file,"w") then do
  496.    writeln(outfile,title""league_title)
  497.    do i=1 to tcount
  498.       writeln(outfile,"*TEAM="teams.i)
  499.       writeln(outfile,"*PLY=0")
  500.       writeln(outfile,"*WIN=0")
  501.       writeln(outfile,"*DRW=0")
  502.       writeln(outfile,"*LST=0")
  503.       writeln(outfile,"*GOF=0")
  504.       writeln(outfile,"*GOA=0")
  505.       if i = tcount then
  506.          writech(outfile,"*PTS=0")
  507.       else
  508.          writeln(outfile,"*PTS=0")
  509.    end
  510.    close(outfile)
  511. end
  512. else do
  513.    say
  514.    say "ERROR :    (Scheduler)"
  515.    say
  516.    say "Cannot create '"league_file || output2_file"'."
  517.    exit
  518. end
  519.                                          /* create report */
  520. select
  521.    when playo = 2 then playo = 'Twice'
  522.    otherwise
  523.       playo = playo || ' times.'
  524. end
  525.  
  526. say
  527. say center("Report for '"league_title"'",78)
  528. say "-------------------------------------------------------------------------------"
  529. say "                                               Created "date('e')" at "time()
  530. say
  531. say "Author : "author
  532. say "Version: "fversion
  533. say "_______________________________________________________________________________"
  534. say
  535. say "Number of teams   : "tcount"                  Number of matches : "matches
  536. say
  537. say "Win   : "ptswin" pts.            Teams Relegated : "reg
  538. say "Draw  : "ptsdrw" pts.            Play Each Team  : "playo
  539. say "Lose  : "ptslse" pts.            Points Per Goals: "ptsgls
  540. say "                          Teams Promoted  : "promo
  541. say
  542. say "Number of Divisions : "ndivs"   (including this league)"
  543. say "Other Divisions     : "divs
  544. say
  545. if autos = 1 then do
  546.    say "Automatic Schedule created from '"autofile||input2_file"'."
  547.    say
  548. end
  549. /*
  550. if matches > 999 then
  551.    say "*   The number of matches exceeds 1000. 'Sortsched' can't handle it!!    *"
  552. */
  553. say "_______________________________________________________________________________"
  554. say
  555. say "Team Listing: "
  556. say "-------------"
  557. say
  558. do i=1 to tcount
  559.    say teams.i
  560. end
  561. say
  562. say "-------------------------------------------------------------------------------"
  563. say
  564.  
  565. exit